package com.hu.web.controller.business;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.hu.common.core.page.TableDataInfo;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.hu.common.annotation.Log;
import com.hu.common.core.controller.BaseController;
import com.hu.common.core.domain.AjaxResult;
import com.hu.common.enums.BusinessType;
import com.hu.system.domain.StockReturn;
import com.hu.system.service.IStockReturnService;
import com.hu.common.utils.poi.ExcelUtil;

/**
 * 退货入库Controller
 *
 * @author hu
 * @date 2025-01-22
 */
@RestController
@RequestMapping("/business/stock/return")
public class StockReturnController extends BaseController {
    @Autowired
    private IStockReturnService stockReturnService;

    /**
     * 查询退货入库列表
     */
    @PreAuthorize("@ss.hasPermi('stock:return:list')")
    @GetMapping("/list")
    public TableDataInfo list(StockReturn stockReturn) {
        startPage();
        List<StockReturn> list = stockReturnService.selectStockReturnList(stockReturn);
        return getDataTable(list);
    }

    /**
     * 导出退货入库列表
     */
    @PreAuthorize("@ss.hasPermi('stock:return:export')")
    @Log(title = "退货入库", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, StockReturn stockReturn) {
        List<StockReturn> list = stockReturnService.selectStockReturnList(stockReturn);
        ExcelUtil<StockReturn> util = new ExcelUtil<StockReturn>(StockReturn.class);
        util.exportExcel(response, list, "退货入库数据");
    }

    /**
     * 获取退货入库详细信息
     */
    @PreAuthorize("@ss.hasPermi('stock:return:query')")
    @GetMapping(value = "/{returnId}")
    public AjaxResult getInfo(@PathVariable("returnId") Long returnId) {
        StockReturn stockReturn = stockReturnService.selectStockReturnByReturnId(returnId);
        if (stockReturn == null) {
            return error("您没有权限查看该退货记录或记录不存在");
        }
        return success(stockReturn);
    }

    /**
     * 新增退货入库
     */
    @PreAuthorize("@ss.hasPermi('stock:return:add')")
    @Log(title = "退货入库", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody StockReturn stockReturn) {
        return toAjax(stockReturnService.insertStockReturn(stockReturn));
    }

    /**
     * 修改退货入库
     */
    @PreAuthorize("@ss.hasPermi('stock:return:edit')")
    @Log(title = "退货入库", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody StockReturn stockReturn) {
        return toAjax(stockReturnService.updateStockReturn(stockReturn));
    }

    /**
     * 删除退货入库
     */
    @PreAuthorize("@ss.hasPermi('stock:return:remove')")
    @Log(title = "退货入库", businessType = BusinessType.DELETE)
    @DeleteMapping("/{returnIds}")
    public AjaxResult remove(@PathVariable Long[] returnIds) {
        return toAjax(stockReturnService.deleteStockReturnByReturnIds(returnIds));
    }
}

